function plusReady() {
    // 是否显示欢迎页
    var _displaywelcome = true, _newversion = false, _mainPage;
    var welcomePage = function () {
        this.init();
        this.bindEvent();
    };

    welcomePage.prototype = {
        // 初始化
        init: function () {
            var model = this;
            // 检查升级包版本
            //this.checkVersion();

            // 判断启动方式
            if ("scheme" == plus.runtime.launcher) {
                var args = plus.runtime.arguments;
                // 处理args参数，如打开新页面等
            }


            // 主页面
            _mainPage = plus.webview.create("../home/main.html", "../home/main.html");
            _mainPage.hide();

            // 获取本地应用资源版本号
            var showwelcome = getLocalStorage(STORAGE_MANAGER.showwelcome);
            if (!showwelcome) {
                // 欢迎页
                $(".banner").show();
                $(".slider").touchSlider({
                    width: $(window).width(),
                    height: $(window).height(),
                    during: 3000,
                    autoplay: false,
                    showindr: true,
                    move: function (currindex, total) {
                        if ((currindex + 1) == total) {
                            $(".focus").hide();
                            setTimeout(function () {
                                $("#btnenter").show();
                            }, 300);
                        } else {
                            $("#btnenter").hide();
                            $(".focus").show();
                        }
                    }
                });
                setTimeout(function () {
                    plus.navigator.closeSplashscreen();
                }, 500);
            } else {
                // 广告倒计时
                $("#advdiv").show();
                var time = 3;
                var timerwatch = setInterval(function () {
                    time = time - 1;
                    if (time == 0) {
                        window.clearInterval(timerwatch);
                        model.enterApp();
                        return;
                    }
                    $("#timerwatch label").text(time + "s");
                }, 1000);
                plus.navigator.closeSplashscreen();
            }
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 欢迎页进入APP
            $("#btnenter").on("click", function () {
                setLocalStorage(STORAGE_MANAGER.showwelcome, true);
                model.enterApp();
            });

            // 广告进入APP
            $("#timerwatch").on("click", function () {
                model.enterApp();
            });
        },

        // 进入APP
        enterApp: function () {
            if (_newversion) {
                return;
            }
            plus.navigator.setFullscreen(false);
            if (!_mainPage) {
                _mainPage = plus.webview.create("../home/main.html", "../home/main.html");
            }
            _mainPage.show("none", 100, function () {
                plus.webview.currentWebview().close();
            });
        },

        // 检查更新
        checkVersion: function () {
            var thisPage = this;

            $.ajax({
                type: 'GET',
                url: CONFIG_MANAGER.webSiteUrl + '/server/appconfig.php'
            }).done(function (data) {
                var appconfig = JSON.parse(data);

                // 获取本地应用资源版本号
                plus.runtime.getProperty(plus.runtime.appid, function (inf) {
                    if (appconfig.version != inf.version) {
                        // 仅无线网下更新
                        var networkType = plus.networkinfo.getCurrentType();
                        if (networkType == plus.networkinfo.CONNECTION_WIFI) {
                            _newversion = true;
                            plus.nativeUI.toast("发现新版本！");
                            thisPage.downloadWgt(appconfig.version);
                        }
                    }

                    // 设置版本信息
                    setLocalStorage(STORAGE_MANAGER.latestversion, {
                        version: appconfig.version,
                        islatest: appconfig.version == inf.version
                    });

                });
            });
        },

        // 下载升级包wgt文件
        downloadWgt: function (version) {
            var thisPage = this;
            var wgtUrl = CONFIG_MANAGER.webSiteUrl + "/download/" + version + ".wgt";
            plus.nativeUI.showWaiting("  下载中...  ");
            plus.downloader.createDownload(wgtUrl, {filename: "_doc/update/"}, function (d, status) {
                if (status == 200) {
                    thisPage.installWgt(d.filename);
                } else {
                    plus.nativeUI.closeWaiting();
                    plus.nativeUI.alert("下载失败！");
                }

            }).start();
        },

        // 更新应用资源
        installWgt: function (path) {
            plus.nativeUI.showWaiting("  更新中...  ");
            plus.runtime.install(path, {}, function () {
                removeLocalStorage(STORAGE_MANAGER.showwelcome);
                plus.nativeUI.closeWaiting();
                plus.nativeUI.toast("更新完成！");
                plus.runtime.restart();
            }, function (e) {
                plus.nativeUI.closeWaiting();
                plus.nativeUI.alert("更新失败[" + e.code + "]：" + e.message);
            });
        }
    };

    var _welcomePage = new welcomePage();
}

if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}
